<?php
//start session, local variables, ect.
include('includes/init.php');

if($_SESSION['logged_in'] != 1)
{
  echo('<SCRIPT>window.close();</SCRIPT>'); 
  exit();
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=<?echo($conf['html_character_set']);?>" />
<link rel="stylesheet" href="<?echo($GLOBALS['theme_path']);?>basic.css" type="text/css" />
<link rel="stylesheet" href="<?echo($GLOBALS['theme_path']);?>css.css" type="text/css" />
<script language="javascript" type="text/javascript">
<!--
function update_uname(uname)
{
	opener.<?echo($_GET['form']);?>.value = uname;
	opener.focus();
	window.close();
}
//-->
</script>
</head>
<body style="margin: 0px 0px 0px 0px;">
<?php
if($_GET['cmd'] == 'buddies')
{
  $db = new dbquery;
  $db->query("SELECT * FROM $conf[prefix_users]users WHERE id=$_SESSION[id]") or $db->err(__FILE__, __LINE__);
  
  $usr = $db->fetch_object();

  $online = null;
  $offline = null;  
  
  $buddies = explode(';', $usr->buddies);

  for($i = 0; $i < (count($buddies)-1); $i++)
  {
    if(is_online($buddies[$i]))
      $online[count($online)]=$buddies[$i];
    else
      $offline[count($offline)]=$buddies[$i];
  }

?>
  <table border="0" width="100%" height="100%" cellpadding="10" cellspacing="0">
  <tr>
  <td>
  <table border="0" width="100%" height="100%" class="tabelka2" cellpadding="4" cellspacing="1">
  <tr>
    <td class="row_3" align="center"><b><span style="font-size: 12px"><?echo(strtoupper($lang['buddies']));?></span></b></td>
  </tr>
  <tr>
    <td class="row_1" align="left" width="100%">
  <p>ONLINE:</p>
  <table border="0">
  <?php 
    for($i = 0; $i < count($online); $i++)
    {
      $u= get_user_by_id($online[$i]);
      echo('<tr><td nowrap align="left" class="row_1"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['online_1'].'" alt="" /><a href="javascript:update_uname(\''.$u->login.'\');">'.read_text_rest($u->login).'</a></td></tr>');
    }
  ?>  
    </table><br></td>
  </tr>
  <tr>
    <td class="row_2" align="left" width="100%">
  
<p>OFFLINE:</p>	
<table border="0">
  <?php 
    for($i = 0; $i < count($offline); $i++)
    {
      $u = get_user_by_id($offline[$i]);
      echo('<tr><td nowrap align="left" class="row_2"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['online_0'].'" alt="" /><a href="javascript:update_uname(\''.$u->login.'\');">'.read_text_rest($u->login).'</a></td></tr>');
    }
  ?>  
    </table><br></td>
  </tr>
  <tr>
    <td class="row_1" align="center" width="100%" height="100%" valign="bottom"><a href="javascript:window.close();"><?echo($lang['close_window']);?></td>
  </tr>
  </table>
  </td>
  </tr>
  </table>
<?php
}
elseif($_GET['cmd'] == 'search')
{
?>
<table border="0" width="100%" height="100%" cellpadding="10" cellspacing="0">
 <tr>
  <td>
  <table border="0" width="100%" height="100%" class="tabelka2" cellpadding="4" cellspacing="1" align="center">
  <tr>
    <td class="row_3" align="center"><b><span style="font-size: 12px"><?echo(strtoupper($lang['find_user']));?></span></b></td>
  </tr>
  <tr>
    <td align="left"><br><FORM ACTION="buddies.php?cmd=search&amp;form=<?echo($_GET['form']);?>" METHOD="POST" NAME="search"><input type="text" name="uname" class="button" value=""> <input type="submit" class="button" value="<?echo($lang['find_user']);?>"></td>
  </tr>
  <tr>
    <td align="left"><?echo($GLOBALS['lang']['global']['hint_search']);?></td>
  </tr>
<?php
 if($_POST['uname'] && $_POST['uname'] != '')
 {
  $db = new dbquery;
  
  $user = str_replace('*', '%', $_POST['uname']);

  $db->query("SELECT * FROM $conf[prefix_users]users WHERE login LIKE '$user' and active = 1 ORDER BY login ASC") or $db->err(__FILE__, __LINE__);
  
  if($db->num_rows() == 0)
     echo('<tr><td><b>'.$lang['no_matches'].'</td></tr>');
  else
  {
    echo('<tr><td><SELECT NAME="users" class="button">');
    while($m = $db->fetch_object())
      echo('<option value="'.read_text_rest($m->login).'">'.read_text_rest($m->login).'</option>');
    echo('</select>&nbsp;<input type="submit" class="button" name="sel" onClick="update_uname(this.form.users.options[this.form.users.selectedIndex].value);return false;" value="'.$lang['select'].'">');
  }
 }
?>
  <tr>
    <td align="center" width="100%" height="100%" valign="bottom"><a href="javascript:window.close();"><?echo($lang['close_window']);?></td>
  </tr>
  </table>
  </form>
  </td>
 </tr>
</table>
<?php
}
?>
</BODY>
</HTML>
